<?php

namespace App\Console\Commands;

use App\Admin\Models\SiteCompany;
use App\Store\Models\Financial\Statements\Liabilities;
use App\Store\Models\Financial\Statements\Moneylog;
use App\Store\Models\Goods\GoodsStockin;
use App\Store\Models\Goods\GoodsStockList;
use App\Store\Models\OldMaterial\Stock;
use App\Store\Models\Order\OrderDeposit;
use App\Store\Models\Order\OrderRecharge;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CheckoutLiabilitiesDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:CheckoutLiabilitiesDay';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '结算年负债数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('结算年负债数据开始');
        $this->checkoutliabilitiesday();
        Log::info('结算年负债数据结束');
        return 0;
    }

    public function checkoutliabilitiesday()
    {
        $ymd = date('Y-m-d', (time()-86400));
        $y = date('Y', (time()-86400));
        $m = date('m', (time()-86400));
        $d = date('d', (time()-86400));
        $start_time = strtotime($y . '-01-01 00:00:00');
        $end_time   = strtotime($ymd . ' 23:59:59');

        $company_list = SiteCompany::where(['audit_status' => 3])->get();
        if (!$company_list) {
            return 0;
        }
        foreach ($company_list as $item) {
            $cp_id = $item->id;

            //1
            $residue_money12 = Moneylog::where(['cp_id' => $cp_id, 'is_del' => 1])->where('receipt_type', '<=', 2)->orderby('id', 'desc')->first()->residue_money ?? 0;
            $residue_money3 = Moneylog::where(['cp_id' => $cp_id, 'is_del' => 1])->where('receipt_type', '=', 3)->orderby('id', 'desc')->first()->residue_money ?? 0;
            $residue_money = $residue_money12 + $residue_money3;

            //17
            $cost1 = GoodsStockList::with(['goods_category' => function ($query) {
                $query->where('is_gram', 1);
            }])->where(['cp_id' => $cp_id, 'is_del' => 1])->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->select(DB::raw('sum((costglod_price+costgram_fee)*total_netglod_weight) as cost_1'))->first()->cost_1 ?? 0;

            $cost2 = GoodsStockList::with(['goods_category' => function ($query) {
                $query->where('is_gram', 2);
            }])->where(['cp_id' => $cp_id, 'is_del' => 1])->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->select(DB::raw('sum(count*piececost_fee) as cost_2'))->first()->cost_2 ?? 0;
            $cost = $cost1 + $cost2;

            //18
            $value18 = Stock::where(['cp_id' => $cp_id, 'is_del' => 1])->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->select(DB::raw('sum(piece_weight_new*back_company_price) as value'))->first()->value ?? 0;

            //33
            $paymoney = GoodsStockin::where(['cp_id' => $cp_id])->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->select(DB::raw('sum(paymoney-paymoney_payed) as paymoney'))->first()->paymoney ?? 0;

            //34
            $pay_money = OrderDeposit::where(['cp_id' => $cp_id, 'is_del' => 1, 'audit_status' => 2])->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->select(DB::raw('sum(pay_money) as pay_money'))->first()->pay_money ?? 0;
            $money = OrderRecharge::where(['cp_id' => $cp_id, 'is_del' => 1, 'audit_status' => 2])->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->select(DB::raw('sum(money) as money'))->first()->money ?? 0;
            $money = $pay_money + $money;

            $info = Liabilities::where([
                'cp_id' => $cp_id,
                'year' => $y,
                'month' => $m,
                'day' => $d,
            ])->first();

            if ($info) {
                $info = $info->toArray();

                $value = json_decode($info['value'], true);

                $value['key_1'] = $residue_money;
                $value['key_17'] = $cost;
                $value['key_18'] = $value18;
                $value['key_33'] = $paymoney;
                $value['key_34'] = $money;

                Liabilities::where([
                    'cp_id' => $cp_id,
                    'year' => $y,
                    'month' => $m,
                    'day' => $d,
                ])->update(['value' => json_encode($value, true)]);
            }
        }
    }
}
